﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NeuralNetwork
{
    public class Neuron
    {
        private List<Dendrit> InputSignals;
        public double Axon {get; protected set;}
        private double Sum;         // suma wejść - WE * waga synapsy
        private double EdgeWeight = 0.1;  //  waga progowa

        public int NumberOfInputSignals {get; set;}
        public double Defect { get; set; }

        public Dendrit this[int index] 
        {
            get { return InputSignals[index]; }
            set { InputSignals.Add(value); }
        }

        public Neuron() 
        {
            InputSignals = new List<Dendrit>();
        }

        public void CountAxon(ActivationFunction func) 
        {
            CountSum();
            switch (func) 
            { 
                case NeuralNetwork.ActivationFunction.Sinus :
                    Sinus();
                    break;

                case NeuralNetwork.ActivationFunction.Signum:
                    Signum();
                    break;

                default :
                    Linear();
                    break;
            }
        }

        private void CountSum() 
        {
            double tmp = 0.0;
            foreach (var input in InputSignals) 
            {
                tmp += input.InputSignal * input.SynapseWeight;
            }

            Sum = tmp + EdgeWeight;
        }

        /// Funkcje aktywacji
        private void Sinus() 
        {
            if (Sum < -(Math.PI / 2)) 
            {
                Axon = -1; 
            }
            else if (Sum > -(Math.PI / 2) && Sum < Math.PI / 2)
            {
                Axon = Math.Sin(Sum);
            }
            else
            {
                Axon = 1;
            }
        }

        private void Signum()
        {
            double alfa = 0.5;
            Axon =  1.0/(1.0 + Math.Exp(-alfa * Sum));
            //if (Sum > 0)
            //{
            //    Axon = 1;
            //}
            //else if (Sum == 0)
            //{
            //    Axon = 0;
            //}
            //else
            //{
            //    Axon = -1;
            //}
        }

        private void Linear()
        {
            if (Sum >= 0)
            {
                Axon = 1;
            }
            else
            {
                Axon = 0;
            }
        }
    }
}
